package com.wltt.product.ahuthr.service;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wltt.product.ahuthr.component.AgreementComponent;
import com.wltt.product.ahuthr.component.FlowComponent;
import com.wltt.product.ahuthr.config.exception.BusinessException;
import com.wltt.product.ahuthr.enums.*;
import com.wltt.product.ahuthr.mapper.*;
import com.wltt.product.ahuthr.model.ResultData;
import com.wltt.product.ahuthr.model.RspPage;
import com.wltt.product.ahuthr.model.UserBasicStartSalaryReq;
import com.wltt.product.ahuthr.model.bo.ListUserBasicWithDeptBO;
import com.wltt.product.ahuthr.model.bo.ListUserDetailInfoBO;
import com.wltt.product.ahuthr.model.bo.QueryForEmploymentExamineBO;
import com.wltt.product.ahuthr.model.po.*;
import com.wltt.product.ahuthr.model.vo.*;
import com.wltt.product.ahuthr.model.vo.base.UserAttendanceInfoVO;
import com.wltt.product.ahuthr.model.vo.base.UserEducationVO;
import com.wltt.product.ahuthr.model.vo.base.UserFamilyMemberVO;
import com.wltt.product.ahuthr.model.vo.base.UserSimpleVO;
import com.wltt.product.ahuthr.service.base.BaseServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * @ClassName: UserBasicServiceImpl
 * @Description: 人员基本信息 逻辑服务类<br/>
 * 不做业务校验，参数校验统一放到前置的Service中
 * @Date: 2024-05-17 09:03:14
 * @Author yanggl
 */
@Service(value = "userBasicService")
public class UserBasicServiceImpl extends BaseServiceImpl<UserBasicPO, Long> implements UserBasicService {

    private UserBasicMapper userBasicMapper;

    @Resource
    private DeptMapper deptMapper;

    @Resource
    private UserAttendanceInfoMapper userAttendanceInfoMapper;

    @Resource
    private UserEducationMapper userEducationMapper;

    @Resource
    private UserFamilyMemberMapper userFamilyMemberMapper;

    @Resource
    private SalaryMapper salaryMapper;

    @Resource
    private AgreementComponent agreementComponent;

    @Resource
    private SystemFlowMapper systemFlowMapper;

    @Resource
    private FlowComponent flowComponent;

    //构造方法
    public UserBasicServiceImpl(UserBasicMapper userBasicMapper) {
        super(userBasicMapper);
        this.userBasicMapper = userBasicMapper;
    }


    /**
     * 分页查询
     *
     * @param listUserBasicReq
     * @return
     */
    @Override
    public ResultData listUser(ListUserBasicReq listUserBasicReq) {
        String orderBy = "user_arrival_date desc";
        Page page = PageHelper.startPage(listUserBasicReq.getPageNo(), listUserBasicReq.getPageSize(), orderBy);

        UserBasicPO userBasicPO = new UserBasicPO();
        BeanUtils.copyProperties(listUserBasicReq, userBasicPO);
        userBasicPO.setUserRegisterStatus(UserRegisterStatusEnum.PASS.getCode());

        List<ListUserBasicWithDeptBO> listUserBasicWithDeptBOS = userBasicMapper.listUserBasicWithDept(userBasicPO);
        List<ListUserBasicRsp> rsps = Optional.ofNullable(listUserBasicWithDeptBOS).orElse(new ArrayList<>())
                .stream().map(bo -> {
                    ListUserBasicRsp rsp = new ListUserBasicRsp();
                    BeanUtils.copyProperties(bo, rsp);
                    return rsp;
                }).collect(Collectors.toList());

        RspPage rspPage = new RspPage(listUserBasicReq.getPageNo(), listUserBasicReq.getPageSize(), rsps, page.getTotal());
        return ResultData.success(rspPage);
    }

    /**
     * 分页查询登记列表
     *
     * @param req
     * @return
     */
    @Override
    public ResultData listUserBasicRegister(ListUserBasicRegisterReq req) {
        String orderBy = "user_arrival_date desc";
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize(), orderBy);

        UserBasicPO userBasicPO = new UserBasicPO();
        BeanUtils.copyProperties(req, userBasicPO);

        List<UserBasicPO> userBasicPOS = userBasicMapper.listUserBasicRegister(userBasicPO);
        List<ListUserBasicRegisterRsp> rsps = Optional.ofNullable(userBasicPOS).orElse(new ArrayList<>())
                .stream().map(po -> {
                    ListUserBasicRegisterRsp rsp = new ListUserBasicRegisterRsp();
                    BeanUtils.copyProperties(po, rsp);
                    return rsp;
                }).collect(Collectors.toList());

        RspPage rspPage = new RspPage(req.getPageNo(), req.getPageSize(), rsps, page.getTotal());
        return ResultData.success(rspPage);
    }

    /**
     * 分页查询起薪列表
     *
     * @param req
     * @return
     */
    @Override
    public ResultData listUserBasicStartSalary(ListUserBasicStartSalaryReq req) {
        String orderBy = "user_arrival_date desc";
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize(), orderBy);

        UserBasicPO userBasicPO = new UserBasicPO();
        BeanUtils.copyProperties(req, userBasicPO);

        List<UserBasicPO> userBasicPOS = userBasicMapper.listUserBasicStartSalary(userBasicPO);
        List<ListUserBasicStartSalaryRsp> rsps = Optional.ofNullable(userBasicPOS).orElse(new ArrayList<>())
                .stream().map(po -> {
                    ListUserBasicStartSalaryRsp rsp = new ListUserBasicStartSalaryRsp();
                    BeanUtils.copyProperties(po, rsp);
                    return rsp;
                }).collect(Collectors.toList());

        RspPage rspPage = new RspPage(req.getPageNo(), req.getPageSize(), rsps, page.getTotal());
        return ResultData.success(rspPage);
    }

    /**
     * 分页查询审核列表
     *
     * @param req
     * @return
     */
    @Override
    public ResultData listUserBasicAudit(ListUserBasicAuditReq req) {
        String orderBy = "user_arrival_date desc";
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize(), orderBy);

        UserBasicPO userBasicPO = new UserBasicPO();
        BeanUtils.copyProperties(req, userBasicPO);

        List<UserBasicPO> userBasicPOS = userBasicMapper.listUserBasicAudit(userBasicPO);
        List<ListUserBasicAuditRsp> rsps = Optional.ofNullable(userBasicPOS).orElse(new ArrayList<>())
                .stream().map(po -> {
                    ListUserBasicAuditRsp rsp = new ListUserBasicAuditRsp();
                    BeanUtils.copyProperties(po, rsp);
                    return rsp;
                }).collect(Collectors.toList());

        RspPage rspPage = new RspPage(req.getPageNo(), req.getPageSize(), rsps, page.getTotal());
        return ResultData.success(rspPage);
    }

    /**
     * 导出
     *
     * @param deptId
     * @param userName
     * @param userIdNumber
     * @param jobNumber
     * @return
     */
    @Override
    public void exportUserBasic(Long deptId, String userName, String userIdNumber, String jobNumber, HttpServletResponse response) {
        UserBasicPO userBasicPO = new UserBasicPO();
        userBasicPO.setDeptId(deptId);
        userBasicPO.setUserName(userName);
        userBasicPO.setUserIdNumber(userIdNumber);
        userBasicPO.setUserRegisterStatus(UserRegisterStatusEnum.PASS.getCode());
        userBasicPO.setJobNumber(jobNumber);
        List<ListUserDetailInfoBO> listUserDetailInfoBOS = userBasicMapper.listUserDetailInfo(userBasicPO);
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("人员信息"+System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), ListUserDetailInfoBO.class).autoCloseStream(Boolean.FALSE).sheet("明细")
                    .doWrite(listUserDetailInfoBOS);
        } catch (Exception e) {
            response.reset();
            throw new BusinessException("导出失败,请稍后重试", HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 详情
     *
     * @param userBasicDetailReq
     * @return
     */
    @Override
    public ResultData userBasicDetail(UserBasicDetailReq userBasicDetailReq) {
        Long id = userBasicDetailReq.getId();
        if (null == id) {
            throw new BusinessException("未指定人员", HttpStatus.BAD_REQUEST);
        }

        UserBasicPO userBasicPO = userBasicMapper.queryById(id);
        if (null == userBasicPO) {
            throw new BusinessException("人员不存在", HttpStatus.BAD_REQUEST);
        }
        UserBasicDetailRsp userBasicDetailRsp = new UserBasicDetailRsp();
        BeanUtils.copyProperties(userBasicPO, userBasicDetailRsp);

        // 来校信息
        UserAttendanceInfoPO userAttendanceInfoPO = userAttendanceInfoMapper.queryByUserId(id);
        UserAttendanceInfoVO userAttendanceInfoVO = new UserAttendanceInfoVO();
        if (null != userAttendanceInfoPO) {
            BeanUtils.copyProperties(userAttendanceInfoPO, userAttendanceInfoVO);
        }
        userBasicDetailRsp.setUserAttendanceInfo(userAttendanceInfoVO);

        // 教育经历
        List<UserEducationPO> userEducationPOList = userEducationMapper.queryByUserId(id);
        if (!CollectionUtils.isEmpty(userEducationPOList)) {
            List<UserEducationVO> userEducationVOList = new ArrayList<>();
            for (UserEducationPO userEducationPO : userEducationPOList) {
                UserEducationVO userEducationVO = new UserEducationVO();
                BeanUtils.copyProperties(userEducationPO, userEducationVO);
                userEducationVOList.add(userEducationVO);
            }
            userBasicDetailRsp.setUserEducationVOList(userEducationVOList);
        }

        // 家庭成员信息
        List<UserFamilyMemberPO> userFamilyMemberPOList = userFamilyMemberMapper.queryByUserId(id);
        if (!CollectionUtils.isEmpty(userFamilyMemberPOList)) {
            List<UserFamilyMemberVO> userFamilyMemberVOList = new ArrayList<>();
            for (UserFamilyMemberPO po : userFamilyMemberPOList) {
                UserFamilyMemberVO vo = new UserFamilyMemberVO();
                BeanUtils.copyProperties(po, vo);
                userFamilyMemberVOList.add(vo);
            }
            userBasicDetailRsp.setUserFamilyMemberVOList(userFamilyMemberVOList);
        }

        return ResultData.success(userBasicDetailRsp);
    }

    /**
     * 简单详情
     *
     * @param userSimpleDetailReq
     * @return
     */
    @Override
    public ResultData userSimpleDetail(UserSimpleDetailReq userSimpleDetailReq) {
        Long userId = userSimpleDetailReq.getUserId();
        if (null == userId) {
            throw new BusinessException("未指定人员", HttpStatus.BAD_REQUEST);
        }

        UserBasicPO userBasicPO = userBasicMapper.queryById(userId);
        if (null == userBasicPO) {
            throw new BusinessException("人员不存在", HttpStatus.BAD_REQUEST);
        }
        UserSimpleVO userSimpleVO = new UserSimpleVO();
        BeanUtils.copyProperties(userBasicPO, userSimpleVO);
        if (null != userBasicPO.getDeptId()) {
            DeptPO deptPO = deptMapper.queryById(userBasicPO.getDeptId());
            if (null != deptPO) {
                userSimpleVO.setDeptName(deptPO.getDeptName());
            }
        }
        return ResultData.success(userSimpleVO);
    }

    /**
     * 提交
     *
     * @param submitUserBasicReq
     * @return
     */
    @Override
    @Transactional
    public ResultData submitUserBasic(SubmitUserBasicReq submitUserBasicReq) {
        Long userId = submitUserBasicReq.getUserId();
        if (null == userId) {
            throw new BusinessException("未指定人员", HttpStatus.BAD_REQUEST);
        }
        UserBasicPO userBasicPO = userBasicMapper.queryById(userId);
        if (null == userBasicPO) {
            throw new BusinessException("人员不存在", HttpStatus.BAD_REQUEST);
        }
        if (UserRegisterStatusEnum.ON_APPROVE.getCode() == userBasicPO.getUserRegisterStatus()
                || UserRegisterStatusEnum.PASS.getCode() == userBasicPO.getUserRegisterStatus()) {
            throw new BusinessException("该用户正在审批中或者审批通过", HttpStatus.BAD_REQUEST);
        }

        SystemFlowPO systemFlowPO = systemFlowMapper.queryByCode(FlowTypeEnum.USER_REGISTER.getCode());
        FlowPO flowPO = flowComponent.startFlow(systemFlowPO, new Date(), String.valueOf(userBasicPO.getId()));

        userBasicPO.setRegisterFlowId(flowPO.getId());
        userBasicPO.setUserRegisterStatus(UserRegisterStatusEnum.ON_APPROVE.getCode());
        userBasicMapper.updateById(userBasicPO);
        return ResultData.success();
    }

    /**
     * 更新
     * TODO 校验：身份证号，工号，姓名等
     *
     * @param userBasicDetailReq
     * @return
     */
    @Override
    @Transactional
    public ResultData updateUserBasic(UpdateUserBasicReq userBasicDetailReq) {
        Long id = userBasicDetailReq.getId();
        UserBasicPO userBasicPO = userBasicMapper.queryById(id);
        if (null == userBasicPO) {
            throw new BusinessException("人员不存在", HttpStatus.BAD_REQUEST);
        }
        // todo 通过状态的修改，后面改成需要流程审批
        if (UserRegisterStatusEnum.DRAFT.getCode() != userBasicPO.getUserRegisterStatus() && UserRegisterStatusEnum.PASS.getCode() != userBasicPO.getUserRegisterStatus()) {
            throw new BusinessException("非草稿状态", HttpStatus.BAD_REQUEST);
        }
        BeanUtils.copyProperties(userBasicDetailReq, userBasicPO);
//		userBasicMapper.updateById(userBasicPO);
        userBasicMapper.updateAllColumnById(userBasicPO);

        // 来校信息
        userAttendanceInfoMapper.deleteByUserId(id);
        UserAttendanceInfoVO userAttendanceInfoVO = userBasicDetailReq.getUserAttendanceInfo();
        if (null != userAttendanceInfoVO && (null != userAttendanceInfoVO.getSubDept()
                || null != userAttendanceInfoVO.getSubDept()
                || null != userAttendanceInfoVO.getUserClass()
                || null != userAttendanceInfoVO.getAttendanceStatus()
                || null != userAttendanceInfoVO.getAttendanceStatusRemark()
                || null != userAttendanceInfoVO.getUserPost()
                || null != userAttendanceInfoVO.getPostTime())
                || null != userAttendanceInfoVO.getProfessionalInfo()) {
            UserAttendanceInfoPO userAttendanceInfoPO = new UserAttendanceInfoPO();
            BeanUtils.copyProperties(userAttendanceInfoVO, userAttendanceInfoPO);
            userAttendanceInfoPO.setUserId(id);
            userAttendanceInfoMapper.insertSelective(userAttendanceInfoPO);
        }

        // 教育信息
        userEducationMapper.deleteByUserId(id);
        List<UserEducationVO> userEducationVOList = userBasicDetailReq.getUserEducationVOList();
        if (!CollectionUtils.isEmpty(userEducationVOList)) {
            List<UserEducationPO> pos = new ArrayList<>();
            for (UserEducationVO userEducationVO : userEducationVOList) {
                UserEducationPO userEducationPO = new UserEducationPO();
                BeanUtils.copyProperties(userEducationVO, userEducationPO);
                userEducationPO.setUserId(id);
                pos.add(userEducationPO);
            }
            userEducationMapper.insertRecords(pos);
        }

        // 教育信息
        userFamilyMemberMapper.deleteByUserId(id);
        List<UserFamilyMemberVO> userFamilyMemberVOList = userBasicDetailReq.getUserFamilyMemberVOList();
        if (!CollectionUtils.isEmpty(userFamilyMemberVOList)) {
            List<UserFamilyMemberPO> pos = new ArrayList<>();
            for (UserFamilyMemberVO vo : userFamilyMemberVOList) {
                UserFamilyMemberPO po = new UserFamilyMemberPO();
                BeanUtils.copyProperties(vo, po);
                po.setUserId(id);
                pos.add(po);
            }
            userFamilyMemberMapper.insertRecords(pos);
        }

        return ResultData.success();
    }

    /**
     * 新增
     *  TODO 校验：身份证号，工号，姓名等
     *
     * @param addUserBasicReq
     * @return
     */
    @Override
    @Transactional
    public ResultData addUserBasic(AddUserBasicReq addUserBasicReq) {
        if (null == addUserBasicReq.getJobNumber()) {
            throw new BusinessException("工号不能为空", HttpStatus.BAD_REQUEST);
        }

        UserBasicPO check1 = userBasicMapper.queryByJobNumber(addUserBasicReq.getJobNumber());
        if (null != check1) {
            throw new BusinessException("工号已存在", HttpStatus.BAD_REQUEST);
        }

//        if (null != addUserBasicReq.getUserIdNumber()) {
//            UserBasicPO check2 = userBasicMapper.queryByIdNumber(addUserBasicReq.getUserIdNumber());
//            if (null != check2) {
//                throw new BusinessException("身份证号已存在", HttpStatus.BAD_REQUEST);
//            }
//        }

        UserBasicPO userBasicPO = new UserBasicPO();
        BeanUtils.copyProperties(addUserBasicReq, userBasicPO);
        userBasicPO.setUserRegisterStatus(UserRegisterStatusEnum.DRAFT.getCode());
        userBasicPO.setUserSalaryStatus(UserSalaryStatusEnum.DRAFT.getCode());
        userBasicMapper.insertSelective(userBasicPO);

        // 来校信息
        UserAttendanceInfoVO userAttendanceInfoVO = addUserBasicReq.getUserAttendanceInfo();
        if (null != userAttendanceInfoVO && (null != userAttendanceInfoVO.getSubDept()
                || null != userAttendanceInfoVO.getSubDept()
                || null != userAttendanceInfoVO.getUserClass()
                || null != userAttendanceInfoVO.getAttendanceStatus()
                || null != userAttendanceInfoVO.getAttendanceStatusRemark()
                || null != userAttendanceInfoVO.getUserPost()
                || null != userAttendanceInfoVO.getPostTime())
                || null != userAttendanceInfoVO.getProfessionalInfo()) {
            UserAttendanceInfoPO userAttendanceInfoPO = new UserAttendanceInfoPO();
            BeanUtils.copyProperties(userAttendanceInfoVO, userAttendanceInfoPO);
            userAttendanceInfoPO.setUserId(userBasicPO.getId());
            userAttendanceInfoMapper.insertSelective(userAttendanceInfoPO);
        }

        // 教育信息
        List<UserEducationVO> userEducationVOList = addUserBasicReq.getUserEducationVOList();
        if (!CollectionUtils.isEmpty(userEducationVOList)) {
            List<UserEducationPO> pos = new ArrayList<>();
            for (UserEducationVO userEducationVO : userEducationVOList) {
                UserEducationPO userEducationPO = new UserEducationPO();
                BeanUtils.copyProperties(userEducationVO, userEducationPO);
                userEducationPO.setUserId(userBasicPO.getId());
                pos.add(userEducationPO);
            }
            userEducationMapper.insertRecords(pos);
        }

        // 教育信息
        List<UserFamilyMemberVO> userFamilyMemberVOList = addUserBasicReq.getUserFamilyMemberVOList();
        if (!CollectionUtils.isEmpty(userFamilyMemberVOList)) {
            List<UserFamilyMemberPO> pos = new ArrayList<>();
            for (UserFamilyMemberVO vo : userFamilyMemberVOList) {
                UserFamilyMemberPO po = new UserFamilyMemberPO();
                BeanUtils.copyProperties(vo, po);
                po.setUserId(userBasicPO.getId());
                pos.add(po);
            }
            userFamilyMemberMapper.insertRecords(pos);
        }

        return ResultData.success();
    }

    /**
     * 删除
     * TODO 校验薪资/进修等数据
     *
     * @param deleteUserBasicReq
     * @return
     */
    @Override
    @Transactional
    public ResultData deleteUserBasic(DeleteUserBasicReq deleteUserBasicReq) {
        Long id = deleteUserBasicReq.getId();
        if (null == id) {
            return ResultData.success();
        }
        SalaryPO salaryPO = salaryMapper.queryByUserId(id);
        if (null != salaryPO) {
            throw new BusinessException("该人员已起薪，不可删除", HttpStatus.BAD_REQUEST);
        }


        // 来校信息
        userAttendanceInfoMapper.deleteByUserId(id);

        // 教育信息
        userEducationMapper.deleteByUserId(id);

        // 教育信息
        userFamilyMemberMapper.deleteByUserId(id);

        userBasicMapper.deleteById(id);

        // 删除合同
        agreementComponent.deleteAgreementByUserId(id);

        return ResultData.success();
    }

    /**
     * 查询聘期考核人员
     *
     * @param queryForEmploymentExamineReq
     * @return
     */
    @Override
    public ResultData queryForEmploymentExamine(QueryForEmploymentExamineReq queryForEmploymentExamineReq) {
        String orderBy = "remind_datetime asc";
        Page page = PageHelper.startPage(queryForEmploymentExamineReq.getPageNo(), queryForEmploymentExamineReq.getPageSize(), orderBy);
        List<QueryForEmploymentExamineBO> queryForEmploymentExamineBOList = userBasicMapper.queryForEmploymentExamine(queryForEmploymentExamineReq);
        List<QueryForEmploymentExamineRsp> rsps = Optional.ofNullable(queryForEmploymentExamineBOList).orElse(new ArrayList<>())
                .stream().map(bo -> {
                    QueryForEmploymentExamineRsp rsp = new QueryForEmploymentExamineRsp();
                    BeanUtils.copyProperties(bo, rsp);
                    return rsp;
                }).collect(Collectors.toList());

        RspPage rspPage = new RspPage(queryForEmploymentExamineReq.getPageNo(), queryForEmploymentExamineReq.getPageSize(), rsps, page.getTotal());
        return ResultData.success(rspPage);

    }

    /**
     * 审核人员信息
     *
     * @param auditUserBasicRegisterReq
     * @return
     */
    @Override
    @Transactional
    public ResultData auditUserBasicRegister(AuditUserBasicRegisterReq auditUserBasicRegisterReq) {
        Long userId = auditUserBasicRegisterReq.getUserId();
        if (null == userId) {
            throw new BusinessException("未指定人员", HttpStatus.BAD_REQUEST);
        }
        Integer dealResult = auditUserBasicRegisterReq.getDealResult();
        if (null == dealResult) {
            throw new BusinessException("未指定处理结果", HttpStatus.BAD_REQUEST);
        }
        String rejectReason = auditUserBasicRegisterReq.getRejectReason();
        if (FlowRecordDealResultEnum.REJECT.getCode() == auditUserBasicRegisterReq.getDealResult() &&  null == rejectReason) {
            throw new BusinessException("未指定拒绝原因", HttpStatus.BAD_REQUEST);
        }
        UserBasicPO userBasicPO = userBasicMapper.queryById(userId);
        if (null == userBasicPO) {
            throw new BusinessException("人员不存在", HttpStatus.BAD_REQUEST);
        }
        if (UserRegisterStatusEnum.ON_APPROVE.getCode() != userBasicPO.getUserRegisterStatus()) {
            throw new BusinessException("无进行中的审批", HttpStatus.BAD_REQUEST);
        }
        Long registerFlowId = userBasicPO.getRegisterFlowId();
        if (null == registerFlowId) {
            throw new BusinessException("该用户的登记审批流数据错误", HttpStatus.BAD_REQUEST);
        }

        Date date = new Date();
        FlowStatusEnum flowStatusEnum = flowComponent.auditFlow(registerFlowId, date, dealResult, rejectReason);
        if (FlowStatusEnum.PASS.getCode() == flowStatusEnum.getCode()) {
            userBasicPO.setUserRegisterStatus(UserRegisterStatusEnum.PASS.getCode());
            userBasicMapper.updateById(userBasicPO);
        } else if (FlowStatusEnum.REJECT.getCode() == flowStatusEnum.getCode()) {
            userBasicPO.setUserRegisterStatus(UserRegisterStatusEnum.REJECT.getCode());
            userBasicMapper.updateById(userBasicPO);
        }
        return ResultData.success();
    }

    /**
     * 人员起薪
     *
     * @param userBasicStartSalaryReq
     * @return
     */
    @Override
    @Transactional
    public ResultData userBasicStartSalary(UserBasicStartSalaryReq userBasicStartSalaryReq) {
        Long userId = userBasicStartSalaryReq.getUserId();
        if (null == userId) {
            throw new BusinessException("未指定人员", HttpStatus.BAD_REQUEST);
        }
        UserBasicPO userBasicPO = userBasicMapper.queryById(userId);
        if (null == userBasicPO) {
            throw new BusinessException("人员不存在", HttpStatus.BAD_REQUEST);
        }
        if (UserRegisterStatusEnum.PASS.getCode() != userBasicPO.getUserRegisterStatus()) {
            throw new BusinessException("人员信息未审核", HttpStatus.BAD_REQUEST);
        }
        if (UserSalaryStatusEnum.ON_APPROVE.getCode() == userBasicPO.getUserSalaryStatus()) {
            throw new BusinessException("该人员正在起薪审批中", HttpStatus.BAD_REQUEST);
        }
        if (UserSalaryStatusEnum.PASS.getCode() == userBasicPO.getUserSalaryStatus()) {
            throw new BusinessException("该人员已起薪", HttpStatus.BAD_REQUEST);
        }

        SystemFlowPO systemFlowPO = systemFlowMapper.queryByCode(FlowTypeEnum.USER_START_SALARY.getCode());
        FlowPO flowPO = flowComponent.startFlow(systemFlowPO, new Date(), String.valueOf(userBasicPO.getId()));

        userBasicPO.setUserSalaryStatus(UserSalaryStatusEnum.ON_APPROVE.getCode());
        userBasicPO.setSalaryFlowId(flowPO.getId());
        userBasicMapper.updateById(userBasicPO);
        return ResultData.success();
    }
}